package dbexp.service;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import dbexp.connection.DbAccessException;
import dbexp.connection.DbConnection;
import dbexp.connection.PoolManagerException;
import dbexp.connection.DbMetaData;
import dbexp.connection.DbQueryException;
import dbexp.connection.DbStateException;
import dbexp.model.DbObject;
import dbexp.model.Function;
import dbexp.model.Package;
import dbexp.model.PackageBody;
import dbexp.model.Procedure;
import dbexp.model.Role;
import dbexp.model.Schema;
import dbexp.model.Sequence;
import dbexp.model.Table;
import dbexp.model.Trigger;
import dbexp.model.User;
import dbexp.model.View;
import dbexp.model.db.DbOT;
import dbexp.model.db.DbUser;

public class DbServiceImpl {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	public String saveData() {// moze byc throws Exception
		return new String("cipcip");
	}

	@SuppressWarnings("null")
	public DbUser checkSession() {
		System.out.println("checkSession");
		HttpServletRequest req = null;
		HttpSession session = req.getSession(false);
		DbUser u = null;
		if (session != null) {
			System.out.println("checkSession:session!=null");
			System.out.println("checkSession:stara sessja");
			u = (DbUser) session.getAttribute(USER);
		}
		if (u == null) {
			u = new DbUser();
		}
		try {
			u = DbMetaData.createConnection(u);
		} catch (PoolManagerException e) {
			e.printStackTrace();
		} catch (DbStateException e) {
			e.printStackTrace();
		} catch (DbAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (u != null) {
			session.setAttribute(USER, u);
		}
		return u;
	}

	@SuppressWarnings("null")
	public void closeSession() {
		HttpServletRequest req = null;
		HttpSession session = req.getSession(false);
		if (session != null) {
			DbUser u = (DbUser) session.getAttribute(USER);
			if (DbMetaData.destroyConnection(u)) {
				session.removeAttribute(USER);
				session.invalidate();
			}
		}

	}

	@SuppressWarnings("null")
	public DbUser createSession(DbUser u)
			throws dbexp.service.DbAccessException {
		if (u != null) {
			System.out.println("createSession");
			System.out.println("username:" + u.getSchemaName() + ",pass:"
					+ u.getPass());
		}
		HttpServletRequest req = null;
		HttpSession session = req.getSession();
		DbUser newUser = null;
		if (!session.isNew()) {
			System.out.println("stara sessja juz istnieje");
			session.removeAttribute(USER);
			session.invalidate();
			session = req.getSession();
		}
		try {
			System.out.println("createConnection przed");
			newUser = DbMetaData.createConnection(u);
			System.out.println("createConnection po");
		} catch (PoolManagerException dme) {
			throw new dbexp.service.DbAccessException(
					"Błąd sterownika bazy danych");
		} catch (DbStateException dse) {
			throw new dbexp.service.DbAccessException(
					"Błąd sprawdzania stanu połączenia");
		} catch (DbAccessException dae) {
			throw new dbexp.service.DbAccessException(dae);
		} catch (SQLException e) {
			throw new dbexp.service.DbAccessException(e);
		}
		if (newUser != null) {
			session.setAttribute(USER, newUser);
		} else {
			throw new dbexp.service.DbAccessException(
					"Nieprawidłowa nazwa lub hasło");
		}
		return newUser;
	}

	@SuppressWarnings("null")
	public DbObject getObject(DbObject o) {
		HttpServletRequest req = null;
		HttpSession session = req.getSession();
		DbObject out = o;
		if (o != null && session.getAttribute(USER) != null) {
			try {
				if (o.isSequence()) {
					Sequence seq = (Sequence) o;
					Sequence so = DbMetaData
							.getSequence((DbUser) session.getAttribute(USER),
									seq.getName(), seq.getSchema());
					if (so != null) {
						return so;
					}
				}
			} catch (PoolManagerException e) {
				e.printStackTrace();
			} catch (DbAccessException e) {
				e.printStackTrace();
			} catch (DbStateException e) {
				e.printStackTrace();
			} catch (DbQueryException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return out;
	}

	@SuppressWarnings("null")
	public DbObject createObject(DbObject o) {
		HttpServletRequest req = null;
		HttpSession session = req.getSession();
		DbObject out = o;
		try {
			if (session.getAttribute(USER) != null) {
				DbUser u = (DbUser) session.getAttribute(USER);
				String info = "Z powodzeniem utworzono obiekt";
				int status = DbConnection.ERROR;
				if (o != null && o.isSequence() && o instanceof Sequence) {
					status = DbMetaData.operateObject(u, (Sequence) o,
							DbConnection.CREATE);
				} else if (o != null && o.isView() && o instanceof View) {
					status = DbMetaData.operateObject(u, (View) o,
							DbConnection.CREATE);
				} else if (o != null && o.isFunction() && o instanceof Function) {
					status = DbMetaData.operateObject(u, (Function) o,
							DbConnection.CREATE);
				} else if (o != null && o.isPackage() && o instanceof Package) {
					status = DbMetaData.operateObject(u, (Package) o,
							DbConnection.CREATE);
				} else if (o != null && o.isPackagebody()
						&& o instanceof PackageBody) {
					status = DbMetaData.operateObject(u, (PackageBody) o,
							DbConnection.CREATE);
				} else if (o != null && o.isProcedure()
						&& o instanceof Procedure) {
					status = DbMetaData.operateObject(u, (Procedure) o,
							DbConnection.CREATE);
				} else if (o != null && o.isRole() && o instanceof Role) {
					status = DbMetaData.operateObject(u, (Role) o,
							DbConnection.CREATE);
				} else if (o != null && o.isSchema() && o instanceof Schema) {
					status = DbMetaData.operateObject(u, (Schema) o,
							DbConnection.CREATE);
				} else if (o != null && o.isTable() && o instanceof Table) {
					status = DbMetaData.operateObject(u, (Table) o,
							DbConnection.CREATE);
				} else if (o != null && o.isTrigger() && o instanceof Trigger) {
					status = DbMetaData.operateObject(u, (Trigger) o,
							DbConnection.CREATE);
				} else if (o != null && o.isUser() && o instanceof User) {
					status = DbMetaData.operateObject(u, (User) o,
							DbConnection.CREATE);
				}
				if (status == DbConnection.NOPERMISION) {
					info = "Nie posiadasz wystarczających uprawnień do utworzenia tego obiektu!";
				} else if (status == DbConnection.EXIST) {
					info = "Obiekt o podanej nazwie już istnieje!";
				}
				if (status == DbConnection.OK) {
					out = DbMetaData.getObject(u, o);
				}
				out.setInfo(info);
				if (status != DbConnection.OK) {
					out.setTyp(DbOT.ERROR);
				}
				return out;
			}
		} catch (PoolManagerException e) {
			out.setInfo(e.getMessage());
		} catch (DbAccessException e) {
			out.setInfo(e.getMessage());
		} catch (DbStateException e) {
			out.setInfo(e.getMessage());
		} catch (DbQueryException e) {
			out.setInfo(e.getMessage());
		} catch (SQLException e) {
			out.setInfo(e.getMessage());
		}

		System.out.println("ob:name:" + o.getString("name") + ",query:"
				+ o.getString("query"));

		return null;
	}

	@SuppressWarnings("null")
	public DbObject deleteObject(DbObject o) {
		HttpServletRequest req = null;
		HttpSession session = req.getSession();
		DbObject out = o;
		try {
			if (session.getAttribute(USER) != null) {
				DbUser u = (DbUser) session.getAttribute(USER);
				String info = "Z powodzeniem usunięto obiekt";
				int status = DbConnection.ERROR;
				if (o != null && o.isSequence() && o instanceof Sequence) {
					status = DbMetaData.operateObject(u, (Sequence) o,
							DbConnection.DELETE);
				} else if (o != null && o.isView() && o instanceof View) {
					status = DbMetaData.operateObject(u, (View) o,
							DbConnection.DELETE);
				} else if (o != null && o.isFunction() && o instanceof Function) {
					status = DbMetaData.operateObject(u, (Function) o,
							DbConnection.DELETE);
				} else if (o != null && o.isPackage() && o instanceof Package) {
					status = DbMetaData.operateObject(u, (Package) o,
							DbConnection.DELETE);
				} else if (o != null && o.isPackagebody()
						&& o instanceof PackageBody) {
					status = DbMetaData.operateObject(u, (PackageBody) o,
							DbConnection.DELETE);
				} else if (o != null && o.isProcedure()
						&& o instanceof Procedure) {
					status = DbMetaData.operateObject(u, (Procedure) o,
							DbConnection.DELETE);
				} else if (o != null && o.isRole() && o instanceof Role) {
					status = DbMetaData.operateObject(u, (Role) o,
							DbConnection.DELETE);
				} else if (o != null && o.isSchema() && o instanceof Schema) {
					status = DbMetaData.operateObject(u, (Schema) o,
							DbConnection.DELETE);
				} else if (o != null && o.isTable() && o instanceof Table) {
					status = DbMetaData.operateObject(u, (Table) o,
							DbConnection.DELETE);
				} else if (o != null && o.isTrigger() && o instanceof Trigger) {
					status = DbMetaData.operateObject(u, (Trigger) o,
							DbConnection.DELETE);
				} else if (o != null && o.isUser() && o instanceof User) {
					status = DbMetaData.operateObject(u, (User) o,
							DbConnection.DELETE);
				}
				if (status == DbConnection.NOPERMISION) {
					info = "Nie posiadasz wystarczających uprawnień do usunieia tego obiektu!";
				} else if (status == DbConnection.EXIST) {
					info = "Obiekt o podanej nazwie nie istnieje!";
				}
				if (status == DbConnection.OK) {
					out = DbMetaData.getObject(u, o);
				}
				out.setInfo(info);
				if (status != DbConnection.OK) {
					out.setTyp(DbOT.ERROR);
				}
				return out;
			}
		} catch (PoolManagerException e) {
			out.setInfo(e.getMessage());
		} catch (DbAccessException e) {
			out.setInfo(e.getMessage());
		} catch (DbStateException e) {
			out.setInfo(e.getMessage());
		} catch (DbQueryException e) {
			out.setInfo(e.getMessage());
		} catch (SQLException e) {
			out.setInfo(e.getMessage());
		}

		System.out.println("ob:name:" + o.getString("name") + ",query:"
				+ o.getString("query"));

		return null;
	}

	final static String USER = "USER";

}
